package com.gitee.amazon.utils;

import com.alibaba.fastjson.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wq
 * @version 1.0
 * @date 2021/03/22 12:06
 */
public class CrawlUtils {
    public static void main(String[] args) {
        Map<Integer, List<JSONObject>> map = new ConcurrentHashMap<>();
        JSONObject adProduct = new JSONObject();
        JSONObject commonProduct = new JSONObject();
        List<JSONObject> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入搜索内容:");
        String keyword = scanner.nextLine();
        //String keyword = "uv light sanitizer";
        Integer page = 1;
        String url = "https://www.amazon.com/s?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&ref=nb_sb_noss_1&k=" + keyword + "&page=" + page;
        try {
            Connection.Response response = Jsoup.connect(url).timeout(10 * 1000)
                    .header("cookie", "session-id=138-4577688-5636860; i18n-prefs=USD; lc-main=zh_CN; sp-cdn=\"L5Z9:CN\"; ubid-main=135-4169340-8898257; session-token=ey6Gstb65PQ/VWp3H+aRrtVd+kLtORHXkc84Q6CnUoQq3qSPrk79hmPen9COPTSdxPVeAmxyTH0HEyNMrmpB7n+ISMxzERfN7Avd9qMaRgXVFiygrx9vAtrXgawCviZ8YZaRxZeEAeV4Wl9R2jiVw6W7hZTHRyfwWiGeFwi4SujdkxqLgpIS/9a5HHPERspS37/8YltNEMERB5a/ykfObUFMrWAIk+ptuGMSCfig9E8aUjTf9HbhXV7qhOjmYqsx; session-id-time=2082787201l; csm-hit=tb:HXWM60W04MF0NP5YYBD9+s-87RMDBHSKA80RXX572XN|1616387426254&t:1616387426254&adb:adblk_no")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36")
                    .method(Connection.Method.GET)
                    .referrer("https://www.amazon.com/s?__mk_zh_CN=%E4%BA%9A%E9%A9%AC%E9%80%8A%E7%BD%91%E7%AB%99&i=aps&k=%E9%AD%85%E6%97%8F&ref=nb_sb_noss_1&url=search-alias%3Daps")
                    .execute();
            if (response.statusCode() == 200) {
                String body = response.body();
                Document document = Jsoup.parse(body);
                Elements elements = document.select("div[data-asin]");
                List<String> adList = new ArrayList<>();
                List<String> commonList = new ArrayList<>();
                for (int i = 5; i < elements.size(); i++) {
                    Elements elementsByClass = elements.get(i).getElementsByClass("a-size-mini a-color-secondary");
                    if (elementsByClass.size() > 0) {
                        String text = elementsByClass.get(0).text();
                        if ("Sponsored".equals(text)) {
                            String adProductValue = elements.get(i).attr("data-asin");
                            if (!StringUtils.isEmpty(adProductValue)) {
                                adList.add(elements.get(i).attr("data-asin"));
                            }
                        } else {
                            String adCommonValue = elements.get(i).attr("data-asin");
                            if (!StringUtils.isEmpty(adCommonValue)) {
                                commonList.add(elements.get(i).attr("data-asin"));
                            }
                        }
                    } else {
                        String adCommonValue = elements.get(i).attr("data-asin");
                        if (!StringUtils.isEmpty(adCommonValue)) {
                            commonList.add(elements.get(i).attr("data-asin"));
                        }
                    }
                }
                adProduct.put("adProduct", adList);
                commonProduct.put("commonProduct", commonList);
                list.add(adProduct);
                list.add(commonProduct);
                if (!url.contains("page=")) {
                    map.put(1, list);
                } else {
                    map.put(page, list);
                }
            }
            System.out.println(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
